package com.szty.lib_pen.oidbluetooth;

import android.graphics.Bitmap;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @author ljm
 * @date 2020/5/18.
 */
public class DotUtils {
    /**
     * (本子铺码) 一英寸里大小 1in=2.54cm=25.40mm
     */
    public static final float IN_SIZE = 25.40f;
    /**
     * 码点规格：1.524mm × 1.524mm
     */
    public static final double DIST_PERUNIT = 1.524d;
    /**
     * OID3S点码规格
     */
    public static final double DIST_PERUNIT_3S = 1.27d;

    /**
     * 计算本子宽高
     *
     * @param bitmap 底图的bitmap
     * @param dpi    打印的dpi
     * @return
     */

    public static Double[] calculateBookSize(Bitmap bitmap, int dpi) {
        Double[] pars = new Double[2];
        int bmpWidth = bitmap.getWidth();
        int bmpHeight = bitmap.getHeight();
        double bookWidth = ((double) bmpWidth / dpi) * IN_SIZE;
        double bookHeight = ((double) bmpHeight / dpi) * IN_SIZE;
        pars[0] = bookWidth;
        pars[1] = bookHeight;
        return pars;
    }

    /**
     * 拼接 x或者y 的dot点
     *
     * @param x  dot整数位
     * @param fx dot小数位
     * @return
     */
    public static float joiningTogether(int x, int fx) {
        float point;
        int temporary = x;
        point = fx;
        point /= 100.0;
        point += temporary;
        return point;
    }

    /**
     * 根据 X点  或者 Y点的dot坐标 转换屏幕坐标
     *
     * @param dotPoint         拼接好的dot座标
     * @param bgSize           绘制view的宽或高
     * @param pageSize         点码纸的宽或高
     * @param dotSpecification 码点规格
     * @return
     */

    public static float getPoint(float dotPoint, int bgSize, double pageSize, double dotSpecification) {
        dotPoint *= (bgSize);
        float ax = (float) (pageSize / dotSpecification);
        dotPoint /= ax;
        return dotPoint;
    }
    public static String parseDeviceName(byte[] scanRecord) {
        String ret = null;
        if(null == scanRecord) {
            return ret;
        }

        ByteBuffer buffer = ByteBuffer.wrap(scanRecord).order(ByteOrder.LITTLE_ENDIAN);
        while (buffer.remaining() > 2) {
            byte length = buffer.get();
            if (length == 0)
                break;

            byte type = buffer.get();
            length -= 1;
            switch (type) {
                case 0x01: // Flags
                    buffer.get(); // flags
                    length--;
                    break;
                case 0x02: // Partial list of 16-bit UUIDs
                case 0x03: // Complete list of 16-bit UUIDs
                case 0x14: // List of 16-bit Service Solicitation UUIDs
                    while (length >= 2) {
                        buffer.getShort();
                        length -= 2;
                    }
                    break;
                case 0x04: // Partial list of 32 bit service UUIDs
                case 0x05: // Complete list of 32 bit service UUIDs
                    while (length >= 4) {
                        buffer.getInt();
                        length -= 4;
                    }
                    break;
                case 0x06: // Partial list of 128-bit UUIDs
                case 0x07: // Complete list of 128-bit UUIDs
                case 0x15: // List of 128-bit Service Solicitation UUIDs
                    while (length >= 16) {
                        long lsb = buffer.getLong();
                        long msb = buffer.getLong();
                        length -= 16;
                    }
                    break;
                case 0x08: // Short local device name
                case 0x09: // Complete local device name
                    byte sb[] = new byte[length];
                    buffer.get(sb, 0, length);
                    length = 0;
                    ret = new String(sb).trim();
                    return ret;
                case (byte) 0xFF: // Manufacturer Specific Data
                    buffer.getShort();
                    length -= 2;
                    break;
                default: // skip
                    break;
            }
            if (length > 0) {
                buffer.position(buffer.position() + length);
            }
        }
        return ret;
    }

}
